﻿
@{
    ViewBag.Title = "ImportWeatherCode";
    Layout = "~/Views/Shared/_ManagerLayout.cshtml";
}

<h2>导入天气代码</h2>
<hr />
@using (Html.BeginForm("Import", "Manager", FormMethod.Post, new { @class = "form-group", enctype = "multipart/form-data", id = "uploadFileForm" }))
{
    <div class="form-group">
        <input type="file" id="file" name="file" accept=".xls,.xlsx" style="width:100%;" class="form-control" />
        <div id="errorMessage" class="alert alert-danger" hidden="hidden"></div>
    </div>
    <div class="form-group">
        <input id="uploadFileButton" type="button" class="btn btn-success" value="导 入" />
    </div>
}
@section Script{
    <script>
            $(function () {
                //标记文件格式是否正确
                var isOk = true;
                $("#file").change(function () {
                    isOk = true;
                    var filepath = $("input[name='file']").val(); // 获取文件路径
                    //获取文件拓展名
                    var extStart = filepath.lastIndexOf(".");
                    var ext = filepath.substring(extStart, filepath.length).toUpperCase();

                    if (ext != ".XLS" && ext != ".XLSX") {
                        $("#errorMessage").html("仅支持上传 .xls 或 .xlsx 格式的Excel文档文件！");
                        isOk = false;
                    }
                    else {
                        var file_size = this.files[0].size;
                        if (file_size > 10 * 1024 * 1024) {
                            $("#errorMessage").html("文件大小不能超过10M哦！");
                            isOk = false;
                        }
                        else {
                            $("#errorMessage").html("");
                        }
                    }
                    if (isOk == true) { //验证通过
                        $("#errorMessage").hide("fast");
                    }
                    else {
                        $("#errorMessage").show("fast");
                    }
                });
                $("#uploadFileButton").on("click", function () {
                    if (isOk == true) {
                        $.ajax({
                            cache: false,
                            data: new FormData($('#uploadFileForm')[0]),
                            url: "/Manager/ImportWeatherCode",
                            type: "post",
                            processData: false,
                            contentType: false,
                            success: function (node) {
                                if (node.state == "ok") {
                                    alert("导入成功！\n");
                                    $("#errorMessage").html(node.message);
                                    $("#errorMessage").show();
                                    window.location = "/Manager/DownloadImportResult";
                                }
                                else {
                                    alert(node.message);
                                }
                            }
                        })
                    }
                    else {
                        alert("文件格式不正确或未选择文件!");
                    }
                });
            })
    </script>
}
